
/*-----------------------------------------------------------------------
 * Copyright (C) 2001 Green Light District Team, Utrecht University 
 *
 * This program (Green Light District) is free software.
 * You may redistribute it and/or modify it under the terms
 * of the GNU General Public License as published by
 * the Free Software Foundation (version 2 or later).
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * See the documentation of Green Light District for further information.
 *------------------------------------------------------------------------*/

package gld.infra;

import gld.*;
import gld.xml.*;
import java.awt.Graphics;
import java.awt.Color;
import java.io.IOException;
import java.util.*;

/**
 * The Bus. Red, Big, Mean and Lean people moving machine.
 *
 * @author Group Datastructures
<<<<<<< Bus.java
 * @version 0.02
=======
 * @version 1.0
 *
 * The Bus. Red, Big, Mean and Lean people moving machine.
>>>>>>> 1.15
 */

public class Bus extends Automobile
{
	protected final int type = RoaduserFactory.getTypeByDesc("Bus");
	protected final int length = 10;
	protected final int speed_max = 40;
	protected int speed_current = 20;
	protected final int acceleration = 6;
	protected final int stopforce = 14;
	
	protected final int platoon_value = 2;

	public Bus(Node new_startNode, Node new_destNode, int pos) {
		super(new_startNode, new_destNode, pos);
	// make color little bit more random
		color = RoaduserFactory.getColorByType(type);
		int r = color.getRed();
		int g = color.getGreen();
		int b = color.getBlue();
		if(r==0) r = (int)(Math.random() * 160);
		if(g==0) g = (int)(Math.random() * 160);
		if(b==0) b = (int)(Math.random() * 160);
		color = new Color(r,g,b);
	}
	
	public Bus() { }
	
	public String getName() { return "Bus"; }
	
	/** Returns the speed of this Roaduser in blocks per cycle */
	public int getSpeed() { return speed_current; }
	public void setSpeed(int newSpeed) { speed_current = newSpeed; }

	public int accelerate(int goalSpeed) 
	{ 
		if (speed_current + acceleration <= speed_max) {
			if (speed_current + acceleration > goalSpeed)
				speed_current = goalSpeed;
			else 
				speed_current = speed_current + acceleration;
		}
		else 
			speed_current = speed_max;
		return speed_current;
	}
	public int deaccelerate(int goalSpeed)
	{
		if (speed_current - stopforce >= 0) {
			if (speed_current - stopforce < goalSpeed)
				speed_current = goalSpeed;
			else 
				speed_current = speed_current - stopforce;
		}
		else
			speed_current = 0;
		return speed_current;
	}
	public int adjustSpeedTo(int goalSpeed) {
		if (speed_current < goalSpeed)
			return accelerate(goalSpeed);
		else return deaccelerate(goalSpeed);
	}
	public int getStopDistance() {
		return (speed_current*speed_current)/(2*stopforce);
	}
	
	public int getPlatoonValue() { return platoon_value; }
	
	public int getLength() { return length; }
	public int getType() { return type; }
	public int getAcceleration() { return acceleration; }

	public void paint(Graphics g, int x, int y, float zf) {}
	
	public void paint(Graphics g, int x, int y, float zf, double dlangle)
	{
		g.setColor(color);
    	double angle = dlangle - Math.toRadians(45.0);
    	int[] cx = new int[4];
    	cx[0] = (int)(Math.round((double)x + Math.sin(angle)));
    	cx[1] = (int)(Math.round((double)x + Math.sin(angle + Math.toRadians(90.0))));
    	cx[2] = (int)(Math.round((double)x + Math.sin(angle + Math.toRadians(180.0) + Math.atan(1 - 1.0 / 3.0)) * 3));
    	cx[3] = (int)(Math.round((double)x + Math.sin(angle + Math.toRadians(270.0) - Math.atan(1 - 1.0 / 3.0)) * 3));

    	int[] cy = new int[4];
    	cy[0] = (int)(Math.round((double)y + Math.cos(angle)));
    	cy[1] = (int)(Math.round((double)y + Math.cos(angle + Math.toRadians(90.0))));
    	cy[2] = (int)(Math.round((double)y + Math.cos(angle + Math.toRadians(180.0) + Math.atan(1 - 1.0 / 3.0)) * 3));
    	cy[3] = (int)(Math.round((double)y + Math.cos(angle + Math.toRadians(270.0) - Math.atan(1 - 1.0 / 3.0)) * 3));

    	g.fillPolygon(cx,cy,4);
	}
	
	// Specific XMLSerializable implementation 
    
	public String getXMLName ()
	{ 	return parentName+".roaduser-bus";
	}
}